/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.EditPanes.Light;

import GUI.Components.BTextField;
import GUI.Components.ColorButton;
import GUI.Components.Float3Panel;
import GUI.EditPanes.EditTaskPane;
import General.CurrentData;
import MonkeyStuff.LightModel;
import com.jme3.light.PointLight;
import com.jme3.math.Vector3f;
import other.Wizard;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.concurrent.Callable;
import javax.swing.JLabel;
import se.datadosen.component.RiverLayout;

/**
 *
 * @author David
 */
public class PointLightTaskPane extends EditTaskPane
{

    private ColorButton colorButton;
    private Float3Panel positionPanel;
    private BTextField radiusTextField;
    private LightModel lightModel;

    /**
     *
     * @param model
     */
    public PointLightTaskPane(LightModel model)
    {
        lightModel = model;
        colorButton = new ColorButton(Wizard.makeColor(lightModel.getLight().getColor()));
        positionPanel = new Float3Panel(((PointLight) lightModel.getLight()).getPosition())
        {
            @Override
            public void setVector(final Vector3f vec)
            {
                ((PointLight) lightModel.getLight()).setPosition(vec);
                CurrentData.getEditorWindow().getB3DApplication().enqueue(new Callable<Void>()
                {
                    public Void call() throws Exception
                    {
                        lightModel.getRepresentative().setLocalTranslation(vec);
                        //lightModel.getSymbol().setLocalTranslation(vec);      doesn't have one, duh
                        return null;
                    }
                });
                super.setVector(vec);
            }
        };
        positionPanel.addFieldKeyListener(new KeyListener()
        {
            public void keyTyped(KeyEvent e)
            {
            }

            public void keyPressed(KeyEvent e)
            {
            }

            public void keyReleased(KeyEvent e)
            {
                if (e.getKeyCode() == KeyEvent.VK_ENTER)
                {
                    updateLight();
                }
            }
        });
        positionPanel.addFieldFocusListener(new FocusListener()
        {
            public void focusGained(FocusEvent e)
            {
            }

            public void focusLost(FocusEvent e)
            {
                updateLight();
            }
        });
        radiusTextField = new BTextField("Float", Float.toString(((PointLight) lightModel.getLight()).getRadius()));
        taskPane.setLayout(new RiverLayout());
        taskPane.setTitle("Point Light");
        add(taskPane, BorderLayout.CENTER);
        applyButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                updateLight();
            }
        });
        taskPane.add("br left", new JLabel("Color:"));
        taskPane.add("tab hfill", colorButton);
        taskPane.add("br left", new JLabel("Position:"));
        taskPane.add("tab hfill", positionPanel);
        taskPane.add("br left", new JLabel("Radius:"));
        taskPane.add("tab hfill", radiusTextField);
        taskPane.add("br right", applyButton);
        updateLight();
    }

    @Override
    public void updateData()
    {
        positionPanel.setVector(((PointLight) lightModel.getLight()).getPosition());
    }

    private void updateLight()
    {
        CurrentData.getEditorWindow().getB3DApplication().enqueue(new Callable<Void>()
        {
            public Void call() throws Exception
            {
                lightModel.getLight().setColor(Wizard.makeColorRGBA(colorButton.getColor()));
                ((PointLight) lightModel.getLight()).setPosition(positionPanel.getVector());
                ((PointLight) lightModel.getLight()).setRadius(Float.parseFloat(radiusTextField.getText()));
                /*lightModel.getSymbol().setLocalTranslation(positionPanel.getVector());
                 lightModel.getSymbol().setMesh(new Sphere(
                 ((Sphere) lightModel.getSymbol().getMesh()).getZSamples(),
                 ((Sphere) lightModel.getSymbol().getMesh()).getRadialSamples(),
                 ((PointLight) lightModel.getLight()).getRadius()));      doesn't have one, duh*/
                lightModel.getRepresentative().setLocalTranslation(positionPanel.getVector());
                return null;
            }
        });
    }
}
